package com.zzh;

import cn.hutool.core.util.ObjectUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @ClassName Test
 * @date 2021/4/5 19:57
 * @Version 1.0
 * @Description //模仿spring
 * @USER root
 */

public class Test {
    public static void main(String[] args) throws IOException {
        //启动、扫描、创建bean（只启动非懒加载的单例bean）
                // 创建 Workbook 对象，读取 Excel 文件
                Workbook workbook = new XSSFWorkbook(new FileInputStream("C:\\Users\\13569\\Desktop\\数据.xlsx"));

                // 获取第一个工作表
                Sheet sheet = workbook.getSheetAt(0);

                // 获取行迭代器
                Iterator<Row> rowIter = sheet.rowIterator();
                Map<String,String> benqi = new HashMap<>();
                Map<String,String> shangqi = new HashMap<>();
                // 遍历行
                while (rowIter.hasNext()) {
                    Row row = rowIter.next();
                    Cell cell1 = row.getCell(0);
                    Cell cell2 = row.getCell(1);
                    String benqiNum = getString(cell1,cell1.getCellType().toString());
                    String benqiData = getString(cell2,cell2.getCellType().toString());
                    benqi.put(benqiNum,benqiData);

                    Cell cell3 = row.getCell(2);
                    Cell cell4 = row.getCell(3);
                    if (ObjectUtil.isNotEmpty(cell3)) {
                        String shangqiNum = getString(cell3,cell3.getCellType().toString());
                        String shangqiData = getString(cell4,cell4.getCellType().toString());
                        shangqi.put(shangqiNum,shangqiData);
                    }


                    // 获取单元格迭代器
                    Iterator<Cell> cellIter = row.cellIterator();
                    // 遍历单元格
                    int index = 0;
                    while (cellIter.hasNext()) {
                        Cell cell = cellIter.next();
                        // 获取单元格内容
                        String numericCellValue = cell.getCellType().toString();
                        String text = getString(cell, numericCellValue);

//                        System.out.print(text + "\t");
                        index++;
                    }
                    index = 0;
                    System.out.println();
                }
        for (Map.Entry<String, String> stringStringEntry : benqi.entrySet()) {
            if (shangqi.containsKey(stringStringEntry.getKey())) {
                String shangqiData1 = shangqi.get(stringStringEntry.getKey());
                String benqiData1 = stringStringEntry.getValue();
                BigDecimal bigDecimal1 = new BigDecimal(shangqiData1);
                BigDecimal bigDecimal2 = new BigDecimal(benqiData1);
                if (bigDecimal1.compareTo(bigDecimal2) != 0) {
                    System.out.println("户号："+stringStringEntry.getKey()+" 上期度数 ：" +shangqiData1 +"本期度数：" +benqiData1);
                }
            }
        }
                // 关闭 Workbook 对象
                workbook.close();
            }

    private static String getString(Cell cell, String numericCellValue) {
        String text = null;
        if (numericCellValue.equals(CellType.STRING.toString())) {
            text = cell.getStringCellValue();
        } else if (numericCellValue.equals(CellType.NUMERIC.toString())) {
            text = String.valueOf(cell.getNumericCellValue());
        }
        return text;
    }

}

